package edison.jcr;

import javax.jcr.ItemNotFoundException;

import org.apache.commons.lang.StringUtils;

public class JcrName {

	private final String namespaceURI;
	private final String localName;

	public JcrName(String nameString) {
		if (StringUtils.countMatches(nameString, "{") == 1
				&& StringUtils.countMatches(nameString, "}") == 1) {
			namespaceURI = StringUtils.substringBetween(nameString, "{", "}").trim();
			localName = StringUtils.substringAfter(nameString, "}").trim();
		}

		throw new IllegalArgumentException(nameString);
	}

	public JcrName(String namespaceURI, String localName) {
		this.namespaceURI = namespaceURI;
		this.localName = localName;
	}

	public String getNamespaceURI() {
		return namespaceURI;
	}

	public String getLocalName() {
		return localName;
	}

	@Override
	public String toString() {
		return String.format("{%s}%s", namespaceURI,localName);
	}

	public static void main(String[] args) throws ItemNotFoundException {
		String s = StringUtils.substringBetween("{sd}fsfs", "{", "}");
		System.out.println(s);
	}
}
